<?php

namespace App\Http\Controllers;

use App\Models\ProductGoods;
use App\Models\Purchase;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends BaseController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 采购需求
     */
    public function purchase(Request $request)
    {
        $data = $request->input();
        if (empty($data['doctor_id'])) {
            return $this->ResultJson(0, '医生ID不能为空!', []);
        }
        if (empty($data['work_id'])) {
            return $this->ResultJson(0, '医护室ID不能为空!', []);
        }if (empty($data['phone'])) {
            return $this->ResultJson(0, '联系方式不能为空!', []);
        }
        if (empty($data['image'])) {
            return $this->ResultJson(0, '图片不能为空!', []);
        }
        if (empty($data['goods_brand'])) {
            return $this->ResultJson(0, '品牌不能为空!', []);
        }
        if (empty($data['goods_type'])) {
            return $this->ResultJson(0, '商品类型不能为空!', []);
        }
        if (empty($data['goods_model'])) {
            return $this->ResultJson(0, '商品型号不能为空!', []);
        }
        if (empty($data['goods_name'])) {
            return $this->ResultJson(0, '商品名称不能为空!', []);
        }
        $res = (new Purchase())->add($data);
        if($res){
            return $this->ResultJson(1, '采购成功!', []);
        }
        return $this->ResultJson(0, '采购失败!', []);
    }

    /**
     * @param Request $request
     * 医师端商品详情
     */
    public function goodsDetail(Request $request)
    {
        $param = $request->input();
        if(empty($param['goods_id'])){
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        $res = (new ProductGoods())->getGoodsDetail($param);
        return $this->ResultJson(1, '获取成功!', $res);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 提交选中商品
     */
    public function addRelateGoods(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作ID不能为空!', []);
        }
        if (empty($param['cate_id'])) {
            return $this->ResultJson(0, '分类ID不能为空!', []);
        }
        if (empty($param['goods_ids'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        $course = (new ProductGoods())->addRelateGoods($param);
        if ($course === true) {
            return $this->ResultJson(1, '添加成功!', []);
        }
        return $this->ResultJson(0, '添加失败!', []);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     * 选品库
     */
    public function getAllGoodsList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医生ID不能为空!', []);
        }
        $res = (new ProductGoods())->getAllGoodsList($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
        return $this->ResultJson(1, '暂无数据', []);
    }

    /**
     * 医护中心商品管理列表
     */
    public function getWorkroomGoods(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $result = (new ProductGoods())->getWorkroomGoods($param);

        return $this->ResultJson(1, '获取成功!', $result);
    }


    /**
     * 商品上下架
     */
    public function setWorkroomGoodsRack(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['source_type'])) {
            return $this->ResultJson(0, '商品来源不能为空!', []);
        }
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        $result = (new ProductGoods())->setWorkroomGoodsRack($param);
        if($result ===true){
            return $this->ResultJson(1, '修改成功!', []);
        }
        return $this->ResultJson(0, $result, []);
    }

    /**
     * 发布商品
     */
    public function addGoods(Request $request)
    {
        $param = $request->input();
        $data = $this->validateGoods($param);
        if ($data === true) {
            $result = (new ProductGoods())->add($param);
            if (is_array($result)){
                return $this->ResultJson(1, "发布成功,等待后台审核",$result);
            }
            return $this->ResultJson(0, "发布失败", []);
        }
        return $this->ResultJson(0, $data, []);
    }

    /**
     * @param $res
     * @return bool|string
     * 验证信息
     */
    public function validateGoods($res)
    {
        if (empty($res['product_name'])) {
            return '商品名称不能为空!';
        }
        if (empty($res['product_unit'])) {
            return '商品单位不能为空!';
        }
        if (empty($res['product_sort_id'])) {
            return '商品分类不能为空!';
        }
        if (empty($res['self_cate_id'])) {
            return '本工作室分类不能为空!';
        }
//        if (empty($res['product_brand_id'])) {
//            return '商品品牌不能为空!';
//        }
        if (empty($res['product_image'])) {
            return '商品图片不能为空!';
        }

//        if (empty($res['product_rests_image'])) {
//            return '其他图片不能为空!';
//        }
        if (empty($res['product_retail_price'])) {
            return '现价不能为空!';
        }
        if (empty($res['product_original_price'])) {
            return '原价不能为空!';
        }
        if (empty($res['product_cost_price'])) {
            return '成本价不能为空!';
        }
        if (empty($res['product_label'])) {
            return '产品标签不能为空!';
        }
        if (empty($res['product_coding'])) {
            return '产品编码不能为空!';
        }
        if (empty($res['product_bar_code'])) {
            return '条形码不能为空!';
        }
        if (empty($res['product_stock'])) {
            return '库存量不能为空!';
        }
        if (empty($res['product_param'])) {
            return '产品参数不能为空!';
        }
        if (empty($res['product_add_user_id'])) {
            return '医生ID不能为空!';
        }
        if (empty($res['supplier_work_id'])) {
            return '工作室id不能为空!';
        }
        return true;
    }

    /**
     * 删除商品
     */
    public function delGoods(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        if (empty($param['source_type'])) {
            return $this->ResultJson(0, '商品来源不能为空!', []);
        }
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        $result = (new ProductGoods())->delGoods($param);
        if ($result === true) {
            return $this->ResultJson(1, "删除成功!", []);
        }
        return $this->ResultJson(0, $result, []);
    }


    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 查看商品
     */

    public function getGoods(Request $request)
    {
        $param = $request->input();
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空!', []);
        }
        $result = (new ProductGoods())->getGoods($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!",$result);
        }
        return $this->ResultJson(0, $result, []);
    }



}
